using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using CMS.Utils.IpSerials.Security;

namespace CMS.Web
{
    public class CDLicence
    {
        public static string GetMasterKey(string fileLicence)
        {
            return GetNodeValue("MasterKey", fileLicence);
        }

        public static string GetLicenseCode(string fileLicence)
        {
            return GetNodeValue("LicenseCode", fileLicence);
        }

        public static bool WebBasedComponent_IsLock(string fileLicence)
        {
            bool result = false;
            string ComputerKey = CMS.Utils.IpSerials.Security.IpKey.GetComputerKey();
            result = IpKey.CheckComputerKey(GetLicenseCode(fileLicence), ComputerKey);
           return (result==false);
        }

        static string GetNodeValue(string xPath, string fileLicence)
        {
            string result = "";
            fileLicence = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/licence.uls");
            //fileLicence = @"d:\My_Working\@_svn_codegoogle\lc-kntc\SourceCode\HS.KNTC.Web\licence.uls";
            System.IO.FileInfo file = new System.IO.FileInfo(fileLicence);

            if (file.Exists)
            {
                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc.Load(fileLicence);
                XmlNode licence = xmlDoc.SelectSingleNode("LicenseInfo/" + xPath);
                result = licence.InnerText;
            }
            return result;
        }
    }
}
